package com.example.memorycardgame;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    // 玩家信息
    private String userName = "默认玩家";
    private String userGender = "未知";
    private String userPhone = "12345678901";
    private int highScore = 0;

    // 控件
    private TextView tvName, tvGender, tvPhone, tvHighScore;
    private Button btnEditInfo, btnStartGame;

    // 用于接收修改信息后的结果
    private ActivityResultLauncher<Intent> editInfoLauncher;
    // 用于接收游戏结束后的最高分
    private ActivityResultLauncher<Intent> gameResultLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        initViews();
        // 初始化Launcher
        initLaunchers();
        // 绑定点击事件
        bindEvents();
        // 更新界面显示
        updateUserInfoDisplay();
    }

    private void initViews() {
        tvName = findViewById(R.id.tv_name);
        tvGender = findViewById(R.id.tv_gender);
        tvPhone = findViewById(R.id.tv_phone);
        tvHighScore = findViewById(R.id.tv_high_score);
        btnEditInfo = findViewById(R.id.btn_edit_info);
        btnStartGame = findViewById(R.id.btn_start_game);
    }

    private void initLaunchers() {
        // 接收信息修改结果
        editInfoLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                        // 更新玩家信息
                        userName = result.getData().getStringExtra("name");
                        userGender = result.getData().getStringExtra("gender");
                        userPhone = result.getData().getStringExtra("phone");
                        // 刷新界面
                        updateUserInfoDisplay();
                    }
                }
        );

        // 接收游戏结果（最高分）
        gameResultLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == RESULT_OK && result.getData() != null) {
                        int newHighScore = result.getData().getIntExtra("highScore", 0);
                        if (newHighScore > highScore) {
                            highScore = newHighScore;
                            updateUserInfoDisplay();
                        }
                    }
                }
        );
    }

    private void bindEvents() {
        // 跳转到信息修改界面
        btnEditInfo.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, EditInfoActivity.class);
            // 传递当前信息
            intent.putExtra("name", userName);
            intent.putExtra("gender", userGender);
            intent.putExtra("phone", userPhone);
            editInfoLauncher.launch(intent);
        });

        // 跳转到翻牌游戏界面
        btnStartGame.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, CardGameActivity.class);
            // 传递当前最高分
            intent.putExtra("currentHighScore", highScore);
            gameResultLauncher.launch(intent);
        });
    }

    // 更新玩家信息显示
    private void updateUserInfoDisplay() {
        tvName.setText("姓名：" + userName);
        tvGender.setText("性别：" + userGender);
        tvPhone.setText("电话：" + userPhone);
        tvHighScore.setText("当前最高分：" + highScore);
    }
}